<template>
  <div>
    <h3>{{msg}} <span v-show="admin.username">当前用户：{{ admin.username }}</span></h3>
    <router-link v-show="!admin.username" :to="{name:'login'}">点击登录</router-link>
    <a v-show="admin.username" href="javascript:void(0)" @click="logout">安全退出</a><hr/>

    <router-link :to="{name:'welcome'}">首页</router-link>
    <router-link :to="{name:'stu'}">学生管理</router-link>
    <router-link :to="{name:'acc'}">帐户管理</router-link>
    <router-link :to="{name:'cate'}">类型管理</router-link>
    <router-link :to="{name:'dept'}">部门管理</router-link>
  </div>
</template>

<script>
import instance from "../utils/request";

export default {
  name: "top",
  data(){
    return {
      msg: "欢迎光临我的主页",
      admin:{}
    };
  },
  methods:{
    logout(){
      // 从本地存储 获取token
      let token = localStorage.getItem("token");

      // 发异步请求，删除登录用户信息
      instance.get("/admin/logout?token="+token)
        .then(res=>{
          // 删除本地存储中的token
          localStorage.removeItem("token")
          this.admin = {}
          this.$router.push({name:"login"});
        });

    }
  },
  created() {
    // 从本地存储 获取token
    let token = localStorage.getItem("token");

    // 发异步请求，获取登录用户信息
    instance.get("/admin/getUserInfo?token="+token)
      .then(res=>{
        this.admin = res.data;
        //console.log(res.data)
      });
  }
}
</script>

<style scoped>

</style>
